package javase.ch02.homeWork.Test1;

public class FactoryDemo {
    public static void main(String[] args) {
        // 工厂模式创建对象
        Shape circle = FactoryDemo.getShape("circle");
        circle.draw(); // 输出：画圆形
        Shape rectangle = FactoryDemo.getShape("rectangle");
        rectangle.draw(); // 输出：画矩形
    }
    public static Shape getShape(String type) {
        if ("circle".equals(type)) {
            return new Circle();
        } else if ("rectangle".equals(type)) {
            return new Rectangle();
        }
        return null;
    }
}
// 产品接口
interface Shape {
    void draw();
}

// 具体产品：圆形
class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("画圆形");
    }
}

// 具体产品：矩形
class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("画矩形");
    }
}
